//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Notification;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Serialization;

namespace OpenLS.Drawing
{
    public class ShapeProperties : ViewModelBase, IOfficeXmlSerializable
    {
        private BlackWhiteMode? _bwMode;
        private EffectProperties _ep;
        private ExtensionElement _extension;
        private FillProperties _fillProp;
        private Geometry _geometry;


        private LineProperties _ln;
        private Scene3D _scene3D;
        private Shape3D _shape3D;


        private Transform2D _xfrm;

        public EffectProperties EffectProperties
        {
            get { return _ep; }
            set
            {
                _ep = value;
                notifyPropertyChanged("EffectProperties");
            }
        }

        public LineProperties LineProperties
        {
            get
            {
                return _ln;
            }
            set
            {
                _ln = value;
                notifyPropertyChanged("LineProperties");
            }
        }

        public Transform2D Xfrm
        {
            get
            {
                return _xfrm;
            }
            set { _xfrm = value; }
        }
        internal Transform2D SafeXfrm
        {
            get
            {
                if (_xfrm == null)
                    _xfrm = new Transform2D();
                return _xfrm;
            }
            set { _xfrm = value; }
        }


        public Geometry Geometry
        {
            get { return _geometry; }
            set
            {
                _geometry = value;
                notifyPropertyChanged("Geometry");
            }
        }

        public FillProperties FillProperties
        {
            get { return _fillProp; }
            set
            {
                _fillProp = value;
                notifyPropertyChanged("FillProperties");
            }
        }

        public Scene3D Scene3D
        {
            get { return _scene3D; }
            set { _scene3D = value; notifyPropertyChanged("Scene3D");}
        }

        public BlackWhiteMode? BlackWhiteMode
        {
            get { return _bwMode; }
            set { _bwMode = value; notifyPropertyChanged("BlackWhiteMode");}
        }

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; notifyPropertyChanged("Extension");}
        }


        public Shape3D Shape3D
        {
            get { return _shape3D; }
            set { _shape3D = value; notifyPropertyChanged("Shape3D");}
        }


        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.ReadAndSwitch("spPr", DrawingConstants.DrawingNamespaceUri, false))
            {
                BlackWhiteMode = c.GetOptionalEnum<BlackWhiteMode>("bwMode");
                _xfrm = c.ReadOptionalElement<Transform2D>("xfrm");
                Geometry = Geometry.ReadGeometry(c);
                FillProperties = FillProperties.ReadFillProperties(c);
                LineProperties = c.ReadOptionalElement<LineProperties>("ln");
                EffectProperties = EffectProperties.ReadEffectProperties(c);
                Scene3D = c.ReadOptionalElement<Scene3D>(Scene3D.xmlName);
                Shape3D = c.ReadOptionalElement<Shape3D>("sp3d");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.WriteAndSwitch("spPr", DrawingConstants.DrawingNamespace))
            {
                c.SetOptionalEnum("bwMode", BlackWhiteMode);
                c.WriteOptionalElement(_xfrm);
                //\\if (Xfrm != null)
                c.WriteOptionalElement(Geometry);
                c.WriteOptionalElement(FillProperties);
                c.WriteOptionalElement(LineProperties);
                c.WriteOptionalElement(EffectProperties);
                c.WriteOptionalElement(Scene3D);
                c.WriteOptionalElement(Shape3D);
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

       

        internal LineProperties EnsureLineProperties()
        {
            if (this.LineProperties == null)
                LineProperties = new LineProperties();
            return LineProperties;
        }
    }
}